package zabbix

import (
	"encoding/json"
	"errors"
)

func HostinterfaceCreateData() (string,map[string]interface{}) {
	method := "hostinterface.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "hostid": "30052",
        "dns": "",
        "ip": "127.0.0.1",
        "main": 0,
        "port": "10050",
        "type": 1,
        "useip": 1
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostinterfaceDeleteData() (string,map[string]interface{}) {
	method := "hostinterface.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
		"30062"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostinterfaceMassremoveData() (string,map[string]interface{}) {
	method := "hostinterface.massremove"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "hostids": [
            "30050",
            "30052"
        ],
        "interfaces": {
            "dns": "",
            "ip": "127.0.0.1",
            "port": "161"
        }
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostinterfaceMassaddData() (string,map[string]interface{}) {
	method := "hostinterface.massadd"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "hosts": [
            {
                "hostid": "30050"
            },
            {
                "hostid": "30052"
            }
        ],
        "interfaces": {
            "dns": "",
            "ip": "127.0.0.1",
            "main": 0,
            "port": "10050",
            "type": 1,
            "useip": 1
        }
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostinterfaceUpdateData() (string,map[string]interface{}) {
	method := "hostinterface.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "interfaceid": "30048",
        "port": "30050"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostinterfaceReplacehostinterfacesData() (string,map[string]interface{}) {
	method := "hostinterface.replacehostinterfaces"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "hostid": "30052",
        "interfaces": {
            "dns": "",
            "ip": "127.0.0.1",
            "main": 1,
            "port": "10050",
            "type": 1,
            "useip": 1
        }
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostinterfaceGetData() (string,map[string]interface{}) {
	method := "hostinterface.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "hostids": "30057"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostinterfaceCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostinterfaceCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostinterfaceDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostinterfaceDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostinterfaceMassremove(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostinterfaceMassremoveData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostinterfaceMassadd(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostinterfaceMassaddData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostinterfaceReplacehostinterface(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostinterfaceReplacehostinterfacesData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostinterfaceUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostinterfaceUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostinterfaceGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostinterfaceGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



